package com.fw.ui.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Searchable;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Results;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;

public class CacheUtils {

	public static final String CACHE_MASTER_DATA = "CACHE_MASTER_DATA";
	public static final String CACHE_QUERY_MASTER_DATA_PER_ITEM = "CACHE_QUERY_MASTER_DATA_PER_ITEM";
	public static final String CACHE_QUERY_SHORT_LIFE = "CACHE_QUERY_SHORT_LIFE";
	public static final String CACHE_GENERAL_OBJECT_CACHE = "CACHE_GENERAL_OBJECT_CACHE";
	public static final String CACHE_USER_SESSION = "CACHE_USER_SESSION";

	static {
		CacheManager.create();
		CacheManager manager = CacheManager.getInstance();

		Cache cache = new Cache(new CacheConfiguration(CACHE_MASTER_DATA,
				5000000)
				.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
				.eternal(false).timeToLiveSeconds(86400)
				.timeToIdleSeconds(86400)
				.diskExpiryThreadIntervalSeconds(86400));
		manager.addCache(cache);

		cache = new Cache(new CacheConfiguration(
				CACHE_QUERY_MASTER_DATA_PER_ITEM, 200)
				.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
				.eternal(false).timeToLiveSeconds(1800).timeToIdleSeconds(900)
				.diskExpiryThreadIntervalSeconds(0));
		manager.addCache(cache);

		CacheConfiguration cacheConfiguration = new CacheConfiguration(
				CACHE_GENERAL_OBJECT_CACHE, 5000000);

		cacheConfiguration
				.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU);
		cacheConfiguration.eternal(false);
		cacheConfiguration.timeToLiveSeconds(0);
		cacheConfiguration.timeToIdleSeconds(0);
		cacheConfiguration.diskExpiryThreadIntervalSeconds(14400);

		Searchable searchable = new Searchable();
		cacheConfiguration.addSearchable(searchable);

		cache = new Cache(cacheConfiguration);
		manager.addCache(cache);

		cache = new Cache(new CacheConfiguration(CACHE_QUERY_SHORT_LIFE,
				1000000)
				.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
				.eternal(false).timeToLiveSeconds(1800).timeToIdleSeconds(900)
				.diskExpiryThreadIntervalSeconds(1500));
		manager.addCache(cache);
	}

	public static CacheManager getManagerInstance() {
		return CacheManager.getInstance();
	}

	public static Object get(String cacheName, String keyName) {
		CacheManager manager = CacheUtils.getManagerInstance();
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			return null;
		}
		Element element = cache.get(keyName);
		if (element != null) {
			Object value = element.getObjectValue();
			return value;
		} else {
			return null;
		}
	}

	public static Results search(String cacheName, String keySearch) {
		CacheManager manager = CacheUtils.getManagerInstance();
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			return null;
		}
		Query query = cache.createQuery();
		query.includeKeys();
		query.includeValues();
		query.addCriteria(Query.KEY.ilike(keySearch));
		query.end();
		Results results = query.execute();
		return results;
	}

	public static void put(String cacheName, String keyName, Object o) {
		CacheManager manager = CacheUtils.getManagerInstance();
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			return;
		}
		Element element = new Element(keyName, o);
		cache.put(element);
	}

	public static void remove(String cacheName, String keyName) {
		CacheManager manager = CacheUtils.getManagerInstance();
		Cache cache = manager.getCache(cacheName);
		if (cache == null) {
			return;
		}
		cache.remove(keyName);
	}

}
